package com.hctforgreen.greenservice.ui.widget;

import java.util.List;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.hctforgreen.greenservice.MessageFirstActivityV2;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.SubmitFeedBackActivityV3;
import com.hctforgreen.greenservice.ctr.DbController;
import com.hctforgreen.greenservice.model.SerieListEntity.SerieEntity;
import com.hctforgreen.greenservice.ui.adapter.DefaultSeriesLstAdapter;

public class DefaultSeriesListPopupView extends PopupWindow {

	private View mConvertView;
	private Activity mActivity;

	private SerieEntity mCurrentFirstEntity;

	private Activity mSelf;

	private boolean bShowDataLst = false;
	public DefaultSeriesListPopupView(Activity activity,
			SerieEntity currentFirstEntity, Activity self, boolean bShowDataLst) {
		super(activity);
		mActivity = activity;

		mCurrentFirstEntity = currentFirstEntity;
		mSelf = self;
		this.bShowDataLst = bShowDataLst;
		initWindow();
	}
	public DefaultSeriesListPopupView(Activity activity,
			SerieEntity currentFirstEntity, Activity self) {
		super(activity);
		mActivity = activity;

		mCurrentFirstEntity = currentFirstEntity;
		mSelf = self;
		initWindow();
	}

	private void initWindow() {
		mConvertView = getLayout();

		this.setContentView(mConvertView);
		this.setBackgroundDrawable(new BitmapDrawable());
		this.setOutsideTouchable(true);
		this.setFocusable(true);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);

		if(mSelf instanceof SubmitFeedBackActivityV3){
			loadDataLst();
		}else if(mSelf instanceof MessageFirstActivityV2){
			if(bShowDataLst){
				loadDataLst();
			}
		}
		
		
	}

	private View getLayout() {
		View layout = null;
		if (mSelf instanceof SubmitFeedBackActivityV3) {
			layout = mActivity.getLayoutInflater().inflate(
					R.layout.popup_default_series_list, null);
			
		}else if (mSelf instanceof MessageFirstActivityV2) {
			layout = mActivity.getLayoutInflater().inflate(
					R.layout.popup_message_top_list, null);
			
		}
		return layout;
	}

	private void loadDataLst() {
		final ListView firstLv = (ListView) mConvertView
				.findViewById(R.id.lst_default_list);

		DbController dbController = new DbController(mActivity);

		List<SerieEntity> entities = dbController.getSerieEntityLst();
		renderDataLst(firstLv, entities);

	}

	private void renderDataLst(ListView firstLv, List<SerieEntity> entities) {

		if (entities == null || entities.size() == 0) {
			return;
		}
		new DefaultSeriesLstAdapter(firstLv, mActivity, entities,
				mCurrentFirstEntity, this);
	}

	public void updateWindow(SerieEntity currentFirstEntity) {
		if (mSelf instanceof SubmitFeedBackActivityV3) {
			((SubmitFeedBackActivityV3) mSelf)
					.updateSeriesBtn(currentFirstEntity);
		} else if (mSelf instanceof MessageFirstActivityV2) {
			((MessageFirstActivityV2) mSelf)
					.updateSecondEntity(currentFirstEntity);
			((MessageFirstActivityV2) mSelf)
					.updateSecondBtn(currentFirstEntity);
		}

	}
}
